<?php
/*
Template Name: Parent-Child-Grandchild
*/
?>
<?php 
			$breadcrumb = get_breadcrumb();
			global $post;
		 	the_post(); 
		 	$post_id =  get_the_ID();
		 	$permalink = get_permalink($post_id);
			$title = get_the_title($post_id);
			$content = $post->post_content;
			$content = apply_filters('the_content', $content);
			if (!$post->post_parent)die();
			$upper_post = get_post($post->post_parent);
			if (!$upper_post->post_parent) {
					$parent_id = $upper_post->ID;					
					$child_id = $post_id;
					$is_child_home = true;
			}
			else {
					$top_post = get_post($upper_post->post_parent);
					if (!$top_post->post_parent){
						$parent_id = $top_post->ID;
						$child_id = $upper_post->ID;
						$grandchild_id = $post_id;
						$is_child_home = false;
					}
					else die();
				}
				$parent_title = get_the_title($parent_id);
				$parent_permalink = get_permalink($parent_id);				
				$grandchild_list = get_grandchild_list($child_id);
?>
<?php get_header();?>

	<div id="container" class="container">
		<div id="content" class="content bg_white" role="main">
			<div class = "breadcrumb">
					<?php echo $breadcrumb;?>	
			</div>		
			<div class = "w100 fl line-aaa"></div>		

			<div class = "page-center">			
				<div class = "sidebar-left">
					<?php 
						$args = array(	
  								'post_type' 	=> 'page',
								'post_status'	=> 'publish',
								'post_parent'	=>	$parent_id,
								'orderby' 		=> 'menu_order',
								'order'			=> 'ASC'
							 );
						$the_query = new WP_Query( $args );
						
						if ($the_query->have_posts()) {
					
					?>
					
					<div class = "page_parent"><a href= "<?php echo $parent_permalink;?>"><?php echo strtoupper($parent_title);?></a></div>
					<div class = "page_child_menu_wide">
						<ul>
					<?php 
						while ($the_query->have_posts()) {
							$the_query->the_post();
							$child_post_id =  get_the_ID();
							$child_permalink = get_permalink($child_post_id);
							$child_title = get_the_title($child_post_id);
					?>
						<li><a href="<?php echo $child_permalink;?>"><?php echo $child_title;?></a>
							<?php if ($child_post_id == $child_id) echo $grandchild_list;?>
						</li>
						<?php }?>
						</ul>
					</div>
					
					<?php }?>
					</div>
					<div class = "center-with-sides">	
						<div class ="page_detail">
							<div class ="page_title">
								<?php echo $title;?>
							</div>
							<div class ="page_content">
								<?php echo $content; ?>
							</div>
						</div>					
					</div>
				</div><!-- .page-center -->
				<div class = "sidebar-right">
					<div class = "page_right">
							<?php get_sidebar('right');?>
					</div>
				</div>
				
			</div><!-- #content -->
		</div><!-- #container -->


<?php get_footer(); ?>
